﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;


namespace SurveillancePro.Base.Converter
{
    #region WorkState2EditAllowedConverter

    /// <summary>
    /// Converts a work state to bool indicating if editing is allowed
    /// </summary>
    public class WorkState2EditAllowedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(bool))
                throw new InvalidOperationException("目标必须是一个布尔类型");

            DataEnum.WorkStateEnum state = (DataEnum.WorkStateEnum)value;
            bool canEdit = false;

            switch (state)
            {
                case DataEnum.WorkStateEnum.NoItemSelected:
                    canEdit = false;
                    break;
                case DataEnum.WorkStateEnum.ItemSelected:
                    canEdit = false;
                    break;
                case DataEnum.WorkStateEnum.Editing:
                    canEdit = true;
                    break;
                case DataEnum.WorkStateEnum.Creating:
                    canEdit = true;
                    break;
            }

            if (parameter != null)
                return !canEdit;
            else
                return canEdit;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    #endregion

    #region EnumToDescriptionConverter

    /// <summary>
    /// Returns the value of the DescriptionAttribute applied to an enum value, or an empty string
    /// if the enum value is not decorated with the attribute.
    /// </summary>
    public class EnumToDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is Enum))
                throw new InvalidOperationException("值必须是枚举类型");
            if (!targetType.IsAssignableFrom(typeof(string)))
                throw new InvalidOperationException("目标类型应该是字符串类型");

            FieldInfo field = value.GetType().GetField(value.ToString());
            object[] attrs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attrs.Length > 0)
            {
                DescriptionAttribute attr = attrs[0] as DescriptionAttribute;
                return attr.Description;
            }

            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("不支持回传");
        }
    }

    #endregion

    #region Visibility2BooleanConverter

    /// <summary>
    /// Convert Visibility to Boolean
    /// </summary>
    public class Visibility2BooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
              object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility)
            {
                if ((Visibility)value == Visibility.Visible)
                    return true;
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType,
              object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value == true)
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }
            else if (value is bool?)
            {
                if (((bool?)value).HasValue && ((bool?)value).Value)
                {
                    return Visibility.Visible;
                }
                return Visibility.Collapsed;
            }

            return Visibility.Collapsed;
        }
    }

    #endregion
}
